﻿#from http://blogs.technet.com/b/heyscriptingguy/archive/2013/08/03/weekend-scripter-use-powershell-to-get-folder-sizes.aspx
Function Get-FolderSize
{

 BEGIN{$fso = New-Object -comobject Scripting.FileSystemObject}

 PROCESS{

    $path = $input.fullname

    $folder = $fso.GetFolder($path)

    $size = $folder.size

    [PSCustomObject]@{'Name' = $path;'Size' = ($size / 1gb) } } }

# even if  run the function with Admin rights, it is likely I will run into errors. So  use the ErrorAction parameter (EA) and set it to SilentlyContinue (0)
#
# Usage
# Get-ChildItem -Directory -Recurse -EA 0 | Get-FolderSize | sort size -Descending
#
#

Function isFolder ($path)
{
   return Test-Path $path -pathtype container ;
#  return  ([IO.FileInfo]$path).Attributes -match 'Directory' ;
# return ((Get-Item $path) -is [System.IO.DirectoryInfo]);
}

Function isFile ($path)
{
    return Test-Path $path -pathtype leaf ;
    #return ((Get-Item $path) -is [System.IO.FileInfo]);
}

function makeSureFileNotExist ($path)
{
    if (Test-Path $path)
    {
        Remove-Item $path
    }
}

function getDirPath ($filePath)
{
     return Split-Path $filePath;

    #It verifies if the path exists or not when -Resolve parameter is specified
   # Split-Path "c:\temp\abc\myproj1\newdata.txt" -Resolve
}

function getFileNameContiainExtension ($filePath)
{
    Split-Path $filePath -Leaf
}

function makeSureDirPathExist ($filePath)
{
    $dirPath = [System.IO.Path]::GetDirectoryName($filePath)
    if ( (-not (Test-Path $dirPath)) -or (isFile $dirPath))
    {
        New-Item $dirPath -type directory
    }
}

function cleanFolder ($folderPath)
{
    Get-Item ([System.IO.Path]::Combine($folderPath,"*")) | %{Remove-Item $_ -Recurse -Force};
}

function saveFileWithUFT8Encoding ($filePath, $content)
{
    $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False);
    [System.IO.File]::WriteAllLines($filePath,$content, $Utf8NoBomEncoding);
}

# 将源文件夹中内容复制到目标文件夹中
function copyFolderToAnother ($srcFolderPath, $tarFolderPath)
{
    $srcFolderPath = [System.IO.Path]::Combine( $srcFolderPath,"*");
    Copy-Item  $srcFolderPath $tarFolderPath -Recurse -Force
}

function getAllFolderExcludeFile ($path)
{
    #列出目录是这样：
    dir $path |where-object{$_.PSIsContainer}
}

#---------------------------------------------- Test --------------------------------------------------------
# ls | ?{isFile $_.FullName}
# ls | ?{isFolder $_.FullName}

# makeSureDirPathExist "c:/test/test/test.txt"

#clearFolder "D:\mavenGenerateJARs"

copyFolderToAnother "D:\Projects\BDCGXJH_FY" "E:\Java\FirstSpringMVC\target\classes\新建文件夹"
